//
// Created by choumei on 2021/9/12.
//

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "MyThread.h"

void MyThread::run() {
    int size = 0;
    struct MsgInfo {
        int32_t msgSize;
        int32_t msgType;
        int32_t msgId;
    };
    MsgInfo info;
    char *audioData = nullptr;
    int id = 0;
//    this->buffer->tcpSocket->write("hello");
    while (true) {
        int defaultSize = 1024;
        audioData = new char[defaultSize];
        int retSize = f->read(audioData, defaultSize);
        if (retSize == 0) {
            break;
        }
        info.msgSize = retSize;
        info.msgId = 0;
        info.msgType = 1;
        auto ret = send(this->buffer->sockfd, (char *) &info.msgSize, 4, 0);
        ret = send(this->buffer->sockfd, (char *) &info.msgType, 4, 0);
        ret = send(this->buffer->sockfd, (char *) &info.msgId, 4, 0);
        ret = send(this->buffer->sockfd, audioData, info.msgSize, 0);
//        auto ret = this->buffer->tcpSocket->write((char *) &info.msgSize, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgType, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgId, 4);
//        ret = this->buffer->tcpSocket->write(audioData, info.msgSize);
//        auto isOK = this->buffer->tcpSocket->flush();
//        bool rec =this->buffer->tcpSocket->waitForBytesWritten();
        id++;
    }
//    QThread::exec();
}

MyThread::MyThread(MyBuffer *buffer) {
    this->buffer = buffer;
    f = new QFile("/home/choumei/CLionProjects/NetClient/resource/audio.pcm");
    f->open(QIODevice::ReadWrite);
    char recvline[4096], sendline[4096];
    struct sockaddr_in servaddr;


}


